<?php

defined('SYSPATH') or die('No direct script access.');

class Controller_Helper_Notification {

    public static function send($id, $messagegoogle, $messageapple) {


        $config = Kohana::$config->load('config')->as_array() + array(
            'php_exec' => '',
            'php_index' => '',
        );
        // Extract configured options
        extract($config, EXTR_SKIP);

        if (substr(php_uname(), 0, 7) == "Windows") {
            $cmd = "$php_exec $php_index --task=Notification --id=$id --messagegoogle=$messagegoogle --messageapple=$messageapple";
            pclose(popen("start /B " . $cmd, "r"));
        }
        //for linux
        else {
            $cmd = "$php_exec $php_index --task=Notification --id=$id --messagegoogle=$messagegoogle --messageapple=$messageapple";
            exec($cmd . " > /dev/null &");
        }
    }

    public static function send_gcm($message) {
        $s_user = new Model_Service_User();
        $a_user = $s_user->get_gcm();
        //send to gcm
        $gcm = new Library_GCM_Basic();
        $s = $gcm->send_notification($a_user, $message);
       
        Log::instance()->add(Log::ERROR, 'SS: ' . $s);
        Log::instance()->write();
    }

    public static function send_apple($message) {
        $s_user = new Model_Service_User();
        $a_apple = $s_user->get_apple();

        $apple = new Library_ApnsPHP_Basic();
        $apple->push($a_apple, $message);
    }

    public static function sent($id, $messagegoogle = '', $messageapple = '') {
        $s_news_la = new Model_Service_New_Language();
        $en_news_en = $s_news_la->get($id, 2)->get_data();
        $en_news_vn = $s_news_la->get($id, 1)->get_data();

        $s_new = new Model_Service_New();
        $en_news = $s_new->load($id)->get_data();

        $data = array();
        if ($en_news->getType() == 'NEWS') {
            $data[] = array('id' => $id,'type' => 'NEWS','language_id' => 1, 'text' => "Có một tin tức mới: " . $en_news_vn->getTitle());
            $data[] = array('id' => $id,'type' => 'NEWS','language_id' => 2, 'text' => "New news: " . $en_news_en->getTitle());
        } else {
            $data[] = array('id' => $id,'type' => 'EVENT','language_id' => 1, 'text' => "Có một sự kiện mới: " . $en_news_vn->getTitle());
            $data[] = array('id' => $id,'type' => 'EVENT','language_id' => 2, 'text' => "New event: " . $en_news_en->getTitle());
        }
        
        $message = array('data' => json_encode($data));

        self::send_gcm($message);


        $data_apple = array();
        if ($en_news->getType() == 'NEWS') {
            $data_apple = array('t1' => "Có một tin tức mới: " . $en_news_vn->getTitle()
                    ,'t2' => "New news: " . $en_news_en->getTitle()
                    ,'id'=>$id
                    ,'link'=>'news/'.$id
                    );
           
        } else {
            $data_apple = array('t1' => "Có một sự kiện mới: " . $en_news_vn->getTitle()
                    ,'t2' => "New event: " . $en_news_en->getTitle()
                    ,'id'=>$id
                    ,'link'=>'event/'.$id
                );
            
        }
      
       self::send_apple((($data_apple)));
      ob_clean();
    }

}
